package com.ayf.payment.game.api.controller.player;

import cn.hutool.core.util.StrUtil;
import com.ayf.payment.game.api.dto.GameInfoDTO;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.MerchantLevelRelationDTO;
import com.ayf.payment.game.api.dto.PartitionDTO;
import com.ayf.payment.game.api.dto.criteria.PartitionSelectCriteria;
import com.ayf.payment.game.api.entity.MerchantConfig;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.GameInfoService;
import com.ayf.payment.game.api.service.MerchantConfigService;
import com.ayf.payment.game.api.service.MerchantLevelRelationService;
import com.ayf.payment.game.api.service.PartitionService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.integral.dto.IntegralGoodDTO;
import com.ayf.payment.integral.dto.MyPageDTO;
import com.ayf.payment.integral.service.remote.IntegralGoodRemoteService;
import com.ayf.payment.integral.vo.IntegralGoodVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zh-liang
 * @Date : 2020-09-14 18:00
 */
@RestController
@RequestMapping("/v/good")
public class MerchantIntegralGoodController {

    private static final String PLATFORM_CODE = System.getenv("GAME_AYFPAY_PLATFORM_CODE");
    private static final String OSS_DOWN_HOST ="https://"+ System.getenv("AYFPAY_INTEGRAL_OSS_BUCKET_NAME")+".oss-cn-beijing.aliyuncs.com/";

    @Reference(version = "1.0.0",check = false)
    private IntegralGoodRemoteService integralGoodRemoteService;
    @Resource
    private MerchantConfigService merchantConfigService;
    @Resource
    private MerchantLevelRelationService merchantLevelRelationService;
    @Resource
    private GameInfoService gameInfoService;
    @Resource
    private PartitionService partitionService;

    /**
     * 商品添加
     */
    @PostMapping("/add")
    public CommonResult<MyPageDTO<IntegralGoodDTO>> add(MerchantDTO merchantDTO, @RequestBody IntegralGoodDTO dto) {
        if (StrUtil.isEmpty(dto.getGoodName())) {
            throw new ParameterException("商品名称不能为空");
        }
        if (StrUtil.isEmpty(dto.getRemark())) {
            throw new ParameterException("商品详情不能为空");
        }
        if (StrUtil.isEmpty(dto.getImgOssId())) {
            throw new ParameterException("图片不能为空");
        }
        if (dto.getVal() == null) {
            throw new ParameterException("商品所需积分不能为空");
        }
        if (dto.getGoodType() == null) {
            throw new ParameterException("请选择商品类型");
        }
        if (dto.getGoodType() == 2) {
            if (dto.getGoodMerchantId() == null) {
                throw new ParameterException("请选择所属商户");
            }
            if (StrUtil.isEmpty(dto.getGameCurrencyNameStr())) {
                throw new ParameterException("请选择可以兑换的游戏币种类");
            }
        }
        dto.setPlatformCode(PLATFORM_CODE);
        dto.setGmId(merchantDTO.getId());
        integralGoodRemoteService.addIntegralGood(dto);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "添加成功");
    }

    /**
     * 商品更新
     */
    @PostMapping("/update")
    public CommonResult<MyPageDTO<IntegralGoodDTO>> update(MerchantDTO merchantDTO, @RequestBody IntegralGoodDTO dto) {
        if (dto.getId() == null) {
            throw new ParameterException("商品id不能为空");
        }
        dto.setPlatformCode(PLATFORM_CODE);
        dto.setGmId(merchantDTO.getId());
        integralGoodRemoteService.updateIntegralGood(dto);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功");
    }

    /**
     * 商品删除
     */
    @PostMapping("/delete")
    public CommonResult<MyPageDTO<IntegralGoodDTO>> delete(MerchantDTO merchantDTO, @RequestBody IntegralGoodDTO dto) {
        if (dto.getId() == null) {
            throw new ParameterException("商品id不能为空");
        }
        dto.setPlatformCode(PLATFORM_CODE);
        dto.setGmId(merchantDTO.getId());
        dto.setStatus(0);
        if (integralGoodRemoteService.deleteIntegralGood(dto)) {
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "删除成功");
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "删除失败，请等待商品下架24小时后再执行删除");
    }

    /**
     * 商品列表查询
     */
    @PostMapping("/list")
    public CommonResult<MyPageDTO<IntegralGoodDTO>> list(MerchantDTO merchantDTO, @RequestBody IntegralGoodVO vo) {
        vo.setPlatformCode(PLATFORM_CODE);
        vo.setGmId(merchantDTO.getId());
        MyPageDTO<IntegralGoodDTO> myPageDTO = integralGoodRemoteService.integralGoodList(vo);
        for (IntegralGoodDTO dto : myPageDTO.getRecords()) {
            dto.setImgOssId(OSS_DOWN_HOST + dto.getImgOssId());
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", myPageDTO);
    }

    /**
     * 商品详情查询
     */
    @GetMapping("/get")
    public CommonResult<IntegralGoodDTO> getInfo(MerchantDTO merchantDTO, @RequestParam("id") Integer id) {
        IntegralGoodDTO dto = integralGoodRemoteService.getIntegralGoodDTO(id);
        if (dto == null || !dto.getGmId().equals(merchantDTO.getId())) {
            return new CommonResult<>(CommonConstants.RETURN_ERROR, "查询失败", dto);
        }
        dto.setImgOssId(OSS_DOWN_HOST + dto.getImgOssId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", dto);
    }

    /**
     * 更新玩家积分联系qq
     */
    @PostMapping("updatePlayerIntegralQq")
    public CommonResult<Object> updatePlayerIntegralStatus(@RequestBody MerchantConfig merchantConfig, MerchantDTO merchantDTO) {
        merchantConfig.setMerchantId(merchantDTO.getId());
        if (merchantConfigService.updatePlayerIntegralQq(merchantConfig)) {
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "修改成功", null);
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "修改失败", null);
    }

    /**
     * 查询玩家积分联系qq
     */
    @GetMapping("/selectConfig")
    public CommonResult<Map<String, Object>> selectConfig(@RequestParam("merchantId") Integer merchantId) {
        MerchantConfig merchantConfig = merchantConfigService.selectOne(merchantId);
        if (merchantConfig == null) {
            return new CommonResult<>(CommonConstants.RETURN_ERROR, "查询失败，merchantId传入错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("playerIntegralQq", merchantConfig.getPlayerIntegralQq());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", map);
    }

    /**
     * 根据一级商户ID获取下级商户列表
     */
    @GetMapping("/merchantIdList")
    public CommonResult<List<MerchantLevelRelationDTO>> merchantIdList(MerchantDTO merchantDTO) {
        List<MerchantLevelRelationDTO> list = merchantLevelRelationService.selectDTOBySupMerchantId(merchantDTO.getId());
        MerchantLevelRelationDTO dto1 = new MerchantLevelRelationDTO();
        dto1.setMerchantId(merchantDTO.getId());
        dto1.setName(merchantDTO.getName());
        list.add(0, dto1);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", list);
    }

    /**
     * 根据商户id查询游戏列表
     */
    @GetMapping("/gameList")
    public CommonResult<List<GameInfoDTO>> gameList(@RequestParam("merchantId") Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("请先选择一个商户");
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", gameInfoService.list(merchantId));
    }

    /**
     * 查询游戏币类型
     */
    @GetMapping("/gameCurrencyList")
    public CommonResult<List<String>> gameCurrencyList(@RequestParam("merchantId") Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("请先选择一个商户");
        }
        List<String> stringList = partitionService.selectGameCurrencyName(merchantId);
        List<String> list = new ArrayList<>();
        for (String s : stringList) {
            String a = s.toLowerCase();
            list.add(a);
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", list);
    }

    /**
     * 查询商品分区列表
     */
    @PostMapping("/partitionList")
    public CommonResult<List<PartitionDTO>> partitionList(@RequestBody PartitionSelectCriteria criteria) {
        if (criteria.getGoodId() == null) {
            throw new ParameterException("goodId不能为空");
        }
        IntegralGoodDTO goodDTO = integralGoodRemoteService.getIntegralGoodDTO(criteria.getGoodId());
        criteria.setMerchantId(goodDTO.getGoodMerchantId());
        List<PartitionDTO> list = partitionService.selectByGameId(criteria);
        List<PartitionDTO> listDTO = new ArrayList<>();
        String[] gameCurrencyNameList = goodDTO.getGameCurrencyNameStr().split("_");
        for (PartitionDTO dto : list) {
            for (String name : gameCurrencyNameList) {
                if (dto.getGameCurrencyName().toLowerCase().equals(name)) {
                    listDTO.add(dto);
                    break;
                }
            }
        }
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", listDTO);
    }
}
